package com.shijie.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.ErpUserService;
import com.shijie.core.AbstractService;
import com.shijie.core.ServiceException;
import com.shijie.domain.entity.Magazines;
import com.shijie.domain.entity.Talents;
import com.shijie.domain.entity.Users;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.domain.enums.EnumInterface.UserType;
import com.shijie.mapper.MagazinesMapper;
import com.shijie.mapper.TalentsMapper;
import com.shijie.mapper.UsersMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-09-08
 */
@Service
public class TalentsService extends AbstractService<Talents> {

    @Resource
    private TalentsMapper talentsMapper;

    @Resource
    private ErpUserService erpUserService;

    @Resource
    private MagazinesMapper magazinesMapper;

    @Resource
    private UsersMapper userMapper;

    public PageInfo<Users> selectTalentsByPage(int page, int pageSize, Users user) {
        PageHelper.startPage(page, pageSize);
        List<Users> list = talentsMapper.selectTalentsByAll(user);
        return new PageInfo<>(list);
    }

    public List<Users> selectTalentsByAll(Users user) {
        return talentsMapper.selectTalentsByAll(user);
    }

    @Transactional
    public void save(Users user) {
        if (user.getId() != null) {
            Talents talent = new Talents(user);
            Example condition = new Example(Talents.class);
            condition.createCriteria().andEqualTo("userId", user.getId());
            talent.setUpdateBy(erpUserService.getUserName());
            talent.setUpdateTime(new Date());
            talentsMapper.updateByConditionSelective(talent, condition);
        } else {
            //TODO
            Users userx = new Users();
            userMapper.insertSelective(userx);
            Talents talent = new Talents(user);
            talent.setUserType(1);
            talent.setIsBind(0);
            talent.setUserId(userx.getId());
            talent.setCreateBy(erpUserService.getUserName());
            talent.setCreateTime(new Date());
            talentsMapper.insertSelective(talent);
            userMapper.deleteByPrimaryKey(userx.getId());
        }

    }

    @Transactional
    public void bind(Users user) {
        if (user.getUserId() == null) {
            throw new ServiceException("绑定的账号Id不能为空!");
        }
        if (user.getTalentId() == null) {
            throw new ServiceException("当前达人Id不能为!");
        }

        Users account = userMapper.detailForUser(user.getUserId());
        Talents talents = talentsMapper.selectByPrimaryKey(user.getTalentId());
        if (account.getUserType() == 1 || account.getIsBind() == 1) {
            throw new ServiceException("当前用户已绑定其他达人");
        }

        //合并信息
        talents.setIsBind(1);
        talents.setUserId(account.getId());
        talents.setFans(talents.getFans() + account.getFans());
        talents.setFavorites(talents.getFavorites() + account.getFavorites());
        talents.setInterests(talents.getInterests() + account.getInterests());
        talents.setScores(talents.getScores() + account.getScores());
        talents.setUpVotes(talents.getUpVotes() + account.getUpVotes());

        talentsMapper.deleteByPrimaryKey(account.getTalentId());
        
        talentsMapper.updateByPrimaryKeySelective(talents);

    }

    public Users getTalentsDetail(Integer talentId) {
        return talentsMapper.detailForTalents(talentId);
    }

    public void sort(Users user) {
        Talents talents = new Talents();
        talents.setId(user.getTalentId());
        talents.setRecommends(user.getRecommends());
        talentsMapper.updateByPrimaryKeySelective(talents);
    }

    public List<Users> getAttentionsTalentById(Integer talentId) {
        Users user = new Users();
        user.setUserType(UserType.TALENT.ordinal());
        user.setTalentId(talentId);
        List<Users> list = talentsMapper.selectTalentsByAll(user);
        return list;
    }


    /**
     * 推荐客户未关注的达人列表
     *
     * @param userId
     * @return
     */
    public List<Talents> recommendTalents(Integer userId, Integer page, Integer pageSize) {
        List<Talents> list = talentsMapper.recommendTalents(userId, page * pageSize, pageSize);

        /*for (Talents talent : list) {
            // 插入用户最近的三个达人文章
            Example condition = new Example(Magazines.class);
            condition.createCriteria().andEqualTo("ownerId", talent.getId()).andEqualTo("type", EnumInterface.EntityType.TALENT_ARTICLE.ordinal())
                    .andEqualTo("status", EnumInterface.Status.VALID.ordinal());
            condition.orderBy("id").desc();
            List<Magazines> magazines = magazinesMapper.selectByCondition(condition);
            if (magazines != null && magazines.size() > 3) {
                talent.setMagazines(magazines.subList(0, 3));
            } else {
                talent.setMagazines(magazines);
            }
        }*/
        return list;
    }

}
